package com.manxi.service.impl;

import com.manxi.constant.StatusConstant;
import com.manxi.entity.Orders;
import com.manxi.mapper.DishMapper;
import com.manxi.mapper.OrderMapper;
import com.manxi.mapper.SetmealMapper;
import com.manxi.mapper.UserMapper;
import com.manxi.service.WorkspaceService;
import com.manxi.vo.BusinessDataVO;
import com.manxi.vo.DishOverViewVO;
import com.manxi.vo.OrderOverViewVO;
import com.manxi.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime beginTime,LocalDateTime endTime) {
        //今日总营业额
        Double turnover = getBusinessCount(beginTime, endTime, Orders.COMPLETED);
        turnover = turnover == null ? 0.0 : turnover;
        //今日总订单数
        Integer totalOrderCount = getOrderCount(beginTime, endTime, null,null);
        //今日有效订单数
        Integer validOrderCount = getOrderCount(beginTime, endTime,null, Orders.COMPLETED);
        //订单完成率
        Double orderCompletionRate = 0.0;
        //平均客单价
        Double unitPrice = 0.0;
        if (totalOrderCount != 0 && turnover != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            unitPrice = turnover/validOrderCount;
        }

        //今日新增用户数
        Integer newUsers = getUserCount(beginTime, endTime);

        //封装数据
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    public OrderOverViewVO overviewOrders() {
        LocalDate dateTime = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(dateTime, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(dateTime, LocalTime.MAX);
        //堂食数量
        Integer tangShiOrders = getOrderCount(beginTime,endTime,Orders.TANG_SHI,Orders.TO_BE_CONFIRMED);
        //自取（打包）数量
        Integer ziQuOrders = getOrderCount(beginTime,endTime,Orders.ZI_QU,Orders.TO_BE_CONFIRMED);
        //配送到家数量
        Integer peiSongOrders = getOrderCount(beginTime,endTime,Orders.PEI_SONG,Orders.TO_BE_CONFIRMED);
        //已完成数量
        Integer completedOrders = getOrderCount(beginTime,endTime,null,Orders.COMPLETED);
        //已取消数量
        Integer cancelledOrders = getOrderCount(beginTime,endTime,null,Orders.CANCELLED);

        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .tangShiOrders(tangShiOrders)
                .ziQuOrders(ziQuOrders)
                .peiSongOrders(peiSongOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .build();
        return orderOverViewVO;
    }

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO overviewDishes() {
        //已启售数量
        Integer sold = dishMapper.countStatus(StatusConstant.ENABLE);
        //已停售数量
        Integer discontinued = dishMapper.countStatus(StatusConstant.DISABLE);
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return dishOverViewVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO overviewSetmeals() {
        //已启售数量
        Integer sold = setmealMapper.countStatus(StatusConstant.ENABLE);
        //已停售数量
        Integer discontinued = setmealMapper.countStatus(StatusConstant.DISABLE);
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return setmealOverViewVO;
    }


    /**
     * 获取今日营业额
     * @param beginTime
     * @param endTime
     * @param status
     * @return
     */
    private Double getBusinessCount(LocalDateTime beginTime, LocalDateTime endTime, Integer status) {
        Map map = new HashMap();
        map.put("begin", beginTime);
        map.put("end", endTime);
        map.put("status", status);
        return orderMapper.sumByMap(map);
    }

    /**
     * 获取今日订单数量
     * @param beginTime
     * @param endTime
     * @param status
     * @return
     */
    private Integer getOrderCount(LocalDateTime beginTime, LocalDateTime endTime,Integer orderType, Integer status) {
        Map map = new HashMap();
        map.put("begin", beginTime);
        map.put("end", endTime);
        map.put("orderType", orderType);
        map.put("status", status);
        return orderMapper.countByMap(map);
    }
    /**
     * 获取今日新增用户数
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    private Integer getUserCount(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = new HashMap();
        map.put("begin", beginTime);
        map.put("end", endTime);
        return userMapper.countByMap(map);
    }


}
